package com.android.tiango.baba.act;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.Selection;
import android.text.Spannable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.entity.LoginEntity;
import com.android.tiango.baba.entity.UserModel;
import com.android.tiango.baba.util.CommUtil;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.ToastUtils;

/**
 * 第三方登录之新浪
 * @author suip
 *
 */
public class ActThreeLogin extends BaBaseActivity implements OnClickListener{

	private EditText mPhone,mPassword;//login 的手机号码账号和密码
	private Button mBtCode;
	private EditText mEtCode;
	private ImageView mIsOpenPassword;//控制密码是否显示为***
	private TextView mTvCode;//验证码重发时间
	private Button mLogin;
	private ImageView mUserCode;//是否阅读并遵守用户守则
	private TextView mText;
	private boolean mIscheck=false;
	private TextView mHave;
	private boolean isCodeTime;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.layout_three_enter);
		
		intiView();
	}

	private void intiView() {
		// TODO Auto-generated method stub
		
		
		mPhone=(EditText) findViewById(R.id.i_enter).findViewById(R.id.et_phone);
		mPassword=(EditText) findViewById(R.id.i_enter).findViewById(R.id.et_password);
		mBtCode=(Button) findViewById(R.id.bt_code);
		mEtCode=(EditText) findViewById(R.id.et_code);
		mIsOpenPassword=(ImageView) findViewById(R.id.i_enter).findViewById(R.id.iv_passwordclose);
		mTvCode=(TextView) findViewById(R.id.tv_code);
		mLogin=(Button) findViewById(R.id.bt_login);
		mUserCode=(ImageView) findViewById(R.id.iv_usercode);
		mText=(TextView) findViewById(R.id.tv_text);
		mHave=(TextView) findViewById(R.id.tv_have);
		
		mIsOpenPassword.setImageResource(R.drawable.password_open2);
		mIsOpenPassword.setOnClickListener(this);
		mHave.setOnClickListener(this);
		
		if("QQ".equals(getIntent().getExtras().getString(Constants.DATA))){
			setTitleName("QQ登录");
			mTitleBar.setBackIcon(R.drawable.back);
			mText.setText(R.string.three_enter2);
		}else if("xinlang".equals(getIntent().getExtras().getString(Constants.DATA))){
			setTitleName("新浪登录");
			mTitleBar.setBackIcon(R.drawable.back);
			mText.setText(R.string.three_enter);
		}
		setBackOnClickListener();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_passwordclose:
			if(mIscheck==false){
				mIsOpenPassword.setImageResource(R.drawable.password_open2);
				mPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
				setCursor(mPassword);
				mIscheck=true;
			}else{
				mIsOpenPassword.setImageResource(R.drawable.password_close2);
				mPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
				setCursor(mPassword);
				mIscheck=false;
			}
			break;
		case R.id.tv_have:
			showActivity(ActRelativeAccount.class, false);
			break;
		case R.id.bt_code:
			if (CommUtil.isMobileNO(mPhone.getText().toString())) {
				if (isCodeTime == false) {
					onLoadCode();
					mBtCode.setTextColor(Color.BLACK);
				} else {
					ToastUtils.showMessage("请稍后点击");
					mBtCode.setTextColor(Color.GRAY);
				}
				showWaitingDialog();
			} else {
				ToastUtils.showMessage("请输入正确手机号码");
			}
			break;
		case R.id.bt_login:
			if (!StringUtils.isEmpty(mPhone.getText().toString())) {
				if (!StringUtils.isEmpty(mPassword.getText().toString())) {
					if (!StringUtils.isEmpty(mEtCode.getText().toString())) {

						if (CommUtil.isMobileNO(mPhone.getText().toString())) {
							showWaitingDialog();
							onLoadCode();
						} else {
							ToastUtils.showMessage("请输入正确手机号码");
						}
					} else {
						ToastUtils.showMessage("验证码不能为空！！");
					}

				} else {
					ToastUtils.showMessage("密码不能为空！！");
				}

			} else {
				ToastUtils.showMessage("手机号码不能为空！！");
			}
			break;
		default:
			break;
		}
	}
	@Override
	protected void onDestroy() {
		isCodeTime = false;
		super.onDestroy();

	};
	private void onLoadCode() {
		// TODO Auto-generated method stub
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams httpparams = new HttpParams();
		httpparams.put("userName", mPhone.getText().toString());
		httpparams.put("sendType", "0");

		client.post(HttpUrl.getUrl(HttpUrl.CODE), httpparams,
				new HttpCallBack() {

					@Override
					public void onHttpSuccess(Object arg0) {
						// TODO Auto-generated method stub
						ToastUtils.showMessage("验证码获取成功！");
						dismissWaitingDialog();
						final long time1 = System.currentTimeMillis();
						isCodeTime = true;
						new Thread(new Runnable() {

							@Override
							public void run() {
								// TODO Auto-generated method stub
								while (isCodeTime) {

									try {
										Thread.sleep(1000);
										long time2 = System.currentTimeMillis();
										if (time2 - time1 <= 60000) {
											Message message = new Message();
											message.what = 0203;
											message.arg1 = (int) (time2 - time1);
											handler.sendMessage(message);
										} else {

										}
									} catch (InterruptedException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							}
						}).start();
					}

					@Override
					public void onHttpStarted() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onHttpFailure(Exception arg0, String arg1) {
						// TODO Auto-generated method stub
						dismissWaitingDialog();
						ToastUtils.showMessage("验证码获取失败！===" + arg1);

						arg0.printStackTrace();
					}
				});
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0203:
				if (60 - (msg.arg1 / 1000) < 0) {
					isCodeTime = false;
				}
				mTvCode.setText(60 - (int) (msg.arg1 / 1000) + "");

				break;

			default:
				break;
			}
		};
	};
	public void setCursor(EditText editText) {
		CharSequence text = editText.getText();
		// Debug.asserts(text instanceof Spannable);
		if (text instanceof Spannable) {
			Spannable spanText = (Spannable) text;
			Selection.setSelection(spanText, text.length());
		}
	}
}
